package com.westar.core.dao;

import com.westar.base.model.AffairInfo;
import com.westar.base.model.AffairMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AffairDao extends BaseDao {


    /**
     * 查询指定类型的办件信息
     * @param nowDateTime
     * @param state
     * @param perFinishState
     * @return
     */
    public List<AffairInfo> listAffairInfoForAutoEdit(String nowDateTime, String state,String[] perFinishState) {
        StringBuffer sql = new StringBuffer( );
        List<Object> args = new ArrayList<>( 2 );
        sql.append( "\n select id,affairId,operator,state,autoHandleTime,affairCode" );
        sql.append( "\n from affairInfo a" );
        sql.append( "\n where 1=1 " );
        this.addSqlWhere( state,sql,args," and state=?" );
        this.addSqlWhereIn( perFinishState,sql,args," and perFinishState in ?" );
        this.addSqlWhere( nowDateTime,sql,args," and autoHandleTime <= ? " );
        sql.append("\n order by autoHandleTime ");
        return this.listQuery( sql.toString(),args.toArray(),AffairInfo.class );
    }

    /**
     * 填充affairCode
     * @param affairInfo
     */
    public void updateAffairCode(AffairInfo affairInfo) {
        StringBuffer sql = new StringBuffer( );
        List<Object> args = new ArrayList<>( 2 );
        sql.append( "\n update affairInfo set affairCode=?,autoHandleTime=?  where affairId=?" );
        args.add(affairInfo.getAffairCode());
        args.add(affairInfo.getAutoHandleTime());
        args.add(affairInfo.getAffairId());
        this.excuteSql( sql.toString(),args.toArray() );
    }

    /**
     * 根据受理编号查询办件信息
     * @param affairCode
     * @return
     */
    public AffairInfo getAffairInfoByCode(String affairCode){
        StringBuffer sql = new StringBuffer( );
        List<Object> args = new ArrayList<>( 2 );
        sql.append( "\n select a.*" );
        sql.append( "\n from affairInfo a" );
        sql.append( "\n where 1=1 " );
        this.addSqlWhere( affairCode,sql,args," and affairCode=?" );
        return (AffairInfo) this.objectQuery(sql.toString(),args.toArray(),AffairInfo.class);
    }


    /**
     * 查询不予受理的数据
     * @return
     */
    public List<AffairInfo> listAffairInfoForReject() {
        StringBuffer sql = new StringBuffer( );
        List<Object> args = new ArrayList<>( 2 );
        sql.append( "\n select id,affairId,operator,state,autoHandleTime,affairCode" );
        sql.append( "\n from affairInfo a" );
        sql.append( "\n where 1=1 " );
        this.addSqlWhere( "0",sql,args," and state=?" );
        this.addSqlWhere( "2",sql,args," and perFinishState <> ? " );
        sql.append("\n order by autoHandleTime ");
        return this.listQuery( sql.toString(),args.toArray(),AffairInfo.class );
    }

    /**
     * 根据受理编号查询办件信息
     * @param affairCode
     * @return
     */
    public AffairMsg getAffairMsgByCode(String affairCode){
        StringBuffer sql = new StringBuffer( );
        List<Object> args = new ArrayList<>( 2 );
        sql.append( "\n select a.*" );
        sql.append( "\n from affairMsg a" );
        sql.append( "\n where 1=1 " );
        this.addSqlWhere( affairCode,sql,args," and affairCode=?" );
        return (AffairMsg) this.objectQuery(sql.toString(),args.toArray(),AffairMsg.class);
    }


}
